<?php

declare (strict_types=1);


namespace App\Models\GameDB\Fantasy;

use App\Models\MainDB\Gift;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class PoolIndependent extends Model
{
    protected $connection = 'mysql_game';
    protected $table = 'lottery_fantasy_pool_independent';
    //protected $fillable = [];
    protected $guarded = [];

    //protected $casts = ['id' => 'int', 'user_id' => 'integer', 'amount' => 'integer', 'balance' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function one(array $filters, bool $lock = false)
    {
        $query = self::query()->where($filters);
        if ($lock) {
            $query->lockForUpdate();
        }
        return $query->first();
    }

    public function listAll(array $filters, $fields)
    {
        return self::query()->where($filters)->select($fields)->get();
    }

    public function getGiftListByIds(array $templateIds, array $fields)
    {
        return self::query()->select($fields)->whereIn('id', $templateIds)->get();
    }

    public function updateData(array $filters, $data)
    {
        return self::query()->where($filters)->update($data);
    }

    public function getListWithPrize(array $filters, array $fields)
    {

        $query = self::query()->select($fields)->leftJoin(app('model_fantasy_pool_independent_prize')->getTable(),function($join){
            $join->on($this->getTable().'.id', '=', app('model_fantasy_pool_independent_prize')->getTable() . ".independent_id")
                ->on($this->getTable().'.group_id', '=', app('model_fantasy_pool_independent_prize')->getTable() . ".group_id");
        });

        return $query->where($filters)->get();
    }

}